package src.cs2340.todoApp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

/**
 * @author Kristian
 * @version 1.0 March 10, 2012
 * Creates a TaskList that holds and can add tasks.
 */
public class TaskList implements Iterable<TaskItem>
{	
	private ArrayList<TaskItem> tasks;
	
	/**
	 * Constructor.
	 * tasks is the array that holds all the TaskItems
	 */
	public TaskList()
	{
		tasks = new ArrayList<TaskItem>();
	}
	
	/**
	 * 
	 * @param itemName The task name.
	 * @param loc Where the task will take place.
	 * @param dueDate When the eask is due.
	 * @param detailDescrip A detailed description of what the task entails.
	 * @param category A classification of the task.
	 */
	public void createTask(String itemName, Location loc, Date dueDate, 
			String detailDescrip, String category, boolean checked)
	{
		tasks.add(new TaskItem(itemName, loc, dueDate, detailDescrip, category, checked));
	}
	
	/**
	 * This method takes in an already existing taskItem and adds it to the end
	 * of the arrayList.
	 * @param taskItem an already existing taskItem.
	 */
	public void createTask(TaskItem taskItem)
	{
		tasks.add(taskItem);
	}
	
	/**
	 * Get method for the encapsulated tasks ArrayList
	 * @return tasks
	 */
	public ArrayList<TaskItem> getTasks() {
		return tasks;
	}
	
	public TaskItem getTask(int index){
		return tasks.get(index);
	}
	
	public TaskItem getTask(String tag){
		for(TaskItem task: tasks){
			if(tag.equals(task.toString())){
				return task;
			}
		}
		return null;
	}
	
	/**
	 * To String for TaskList
	 */
	@Override
	public String toString() {
		return tasks.toString();
	}
	
	public int size() {
		return tasks.size();
	}
	
	/**
	 * Sorts tasks items by date
	 * @return tasks list
	 */
	public ArrayList<TaskItem> sortByDate(Date d) {
		Collections.sort(tasks, new DateComparator());
		if (d != null) {
			ArrayList<TaskItem> ret = new ArrayList<TaskItem>();
			for(TaskItem item : tasks) {
				if (item.getDueDate().compareTo(d) >= 0) {
					ret.add(item);
				}
			}
			return ret;
		}
		return tasks;
	}
	
	/**
	 * Sorts tasks items by category
	 * @return tasks list
	 */
	public ArrayList<TaskItem> sortByCategory(int categorySelection) {
		ArrayList<TaskItem> ret = new ArrayList<TaskItem>();
		switch (categorySelection) {
		case 0:
			for(TaskItem t : tasks) {
				if (t.getCategory().equals("Personal")) {
					ret.add(t);
				}
			}
			return ret;
		case 1:
			for(TaskItem t : tasks) {
				if (t.getCategory().equals("School")) {
					ret.add(t);
				}
			}
			return ret;
		case 2:
			for(TaskItem t : tasks) {
				if (t.getCategory().equals("Work")) {
					ret.add(t);
				}
			}
			return ret;
		}
		Collections.sort(tasks, new CategoryComparator());
		return tasks;
	}

	public ArrayList<TaskItem> getUncheckedTasks() {
		ArrayList<TaskItem> uncheckedList = new ArrayList<TaskItem>();
		for(TaskItem t: tasks){
			if(!t.isChecked()){
				uncheckedList.add(t);
			}
		}
		return uncheckedList;
	}

	@Override
	public Iterator<TaskItem> iterator() {
		return tasks.iterator();
	}
}
